<?php

namespace App\Repositories;

use App\Models\Conference;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ConferenceRepository extends BaseRepository
{
	public function model()
	{
		return Conference::class;
	}

    /**
     * 获取合作公司数据
     * @param $company
     * @param bool $initiator
     * @param bool $project
     * @return $this
     */
	public function getByCompany($company, $user_id, $start_time = false, $end_time = false, $project = false, $type = false, $status = false)
	{
		$query = DB::table('conferences')->where('company_id', '=', $company);
		if ($start_time && $end_time) {
            $query->whereDate('start_time','>=', $start_time)->whereDate('end_time', '<=', $end_time);
		}
		if ($start_time && empty($end_time)) {
            $query->whereDate('start_time','=', $start_time);
        }
        if (empty($start_time) && $end_time) {
            $query->whereDate('end_time','=', $end_time);
        }
		if ($project) {
            $query->where('projects','like', '%'.$project.'%');
        }
        if ($type) {
            $query->where('type', $type);
        }
        if($status) {
		    $now = Carbon::now();
		    $query->where('initiator_id', $user_id);
            switch ($status){
                case 'not_start':
                    $query->whereDate('start_time', '>', $now);
                    break;
                case 'going':
                    $query->whereDate('start_time','<=', $now)->whereDate('end_time', '>=', $now);
                    break;
                case 'complete':
                    $query->whereDate('end_time', '<', $now);
                    break;
            }
        }

		return $query;
	}

    /**
     * 获取状态
     * @param $status
     * @return $this
     */
	public function getByStatus($status)
    {
        $this->where('status', '=', $status);

        return $this;
    }



	protected function serialization(array $attributes)
	{
		// TODO: Implement serialization() method.
	}

}
